//
//  DGNProgressView.swift
//  Digimon
//
//  Created by Vill on 2023/10/25.
//

import UIKit

private var KProgressPadding = 1.0

class DGNProgressView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI(){
        addSubview(self.bordView)
        addSubview(self.tView)
        addSubview(self.tipLbl)
        addSubview(self.progressCount)
        self.tipLbl.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalTo(self.bordView.snp.bottom).offset(PTW(2))
        }
        self.progressCount.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalTo(self.tipLbl)
        }
    }
    
    var progress:CGFloat = 0{
        didSet {
            let maxWidth = SCREENWIDTH - PTW(90) - KProgressPadding*2
            let height = PTW(8) - KProgressPadding*2
            self.tView.frame = CGRect(x: KProgressPadding, y: KProgressPadding, width: maxWidth * progress, height: height)
            DispatchQueue.main.safeAsync {
                self.tView.backgroundColor = UIColor.gradientFromColor(colors: [UIColor.colorWithHex("#C000DD").cgColor,UIColor.colorWithHex("#FF329C").cgColor], direction: .Horizontal, range: maxWidth*self.progress)
            }
            self.progressCount.text = String(format: "%.f%%",progress*100)
        }
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        self.tView.layer.cornerRadius = (PTW(3) - KProgressPadding*2) * 0.5
        self.tView.layer.masksToBounds = true
    }
    
    //边框
    lazy var bordView: UIView = {
        let boader = UIView(frame: CGRect(x: 0, y: 0, width: SCREENWIDTH - PTW(90), height: PTW(8)))
        boader.layer.cornerRadius = PTW(4)
        boader.layer.borderWidth = PTW(0.5)
        boader.layer.borderColor = UIColor.colorWithHex("#FF329C").cgColor
        boader.layer.masksToBounds = true
        return boader
    }()
    
    //进度
    lazy var tView: UIView = {
        let boader = UIView()
        boader.backgroundColor = UIColor.clear
        return boader
    }()
    
    lazy var tipLbl: UILabel = {
        let boader = UILabel()
        boader.text = "Positive reviews"
        boader.textColor = .white
        boader.font = UIFont.pingfangFont(ofSize: PTW(12), weight: .regular)
        return boader
    }()
    
    lazy var progressCount: UILabel = {
        let boader = UILabel()
        boader.textColor = UIColor.white
        boader.font = UIFont.pingfangFont(ofSize: PTW(12), weight: .regular)
        return boader
    }()
    
    
}
